package com.crm.system.controller.situationAware;

import com.crm.common.model.DataResponse;
import com.crm.common.model.DhPageInfo;
import com.crm.common.model.DhResponse;
import com.crm.common.model.Enum.ResponseCodeEnum;
import com.crm.model.vo.situationAware.FinancialNewsVO;
import com.crm.model.vo.situationAware.SpiderDataVO;
import com.crm.system.service.situationAware.SpiderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 态势感知
 *
 * @author yuanyunfeng
 * @date 2021/6/3 8:53
 */
@RestController
@RequestMapping("/spider")
@Slf4j
public class SpiderDataController {

    @Autowired
    private SpiderService spiderService;

    /**
     * 数据中心
     *
     * @param pageIndex   页码
     * @param pageSize    分页大小
     * @param sortName    排序列名字
     * @param sortOrder   排序规则(asc/desc)
     * @param companyName 公司名称
     * @return
     */
    @PostMapping("/data/list")
    public DataResponse spiderData(HttpServletRequest request,
                                   @RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                   @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                   @RequestParam(value = "sortName", required = false) String sortName,//排序列名字
                                   @RequestParam(value = "sortOrder", required = false) String sortOrder,//排序规则(asc/desc)
                                   @RequestParam(value = "companyName", required = true) String companyName
    ) {
        try {
            DhPageInfo<SpiderDataVO> spiderDataList = spiderService.querySpiderData(pageIndex, pageSize, sortName, sortOrder, companyName);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), spiderDataList);
        } catch (Exception e) {
            log.error("spiderData spiderDataList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }


    /**
     * 财经新闻数据爬取
     *
     * @param request
     * @return
     */
    @PostMapping("/insert/financialNews")
    public DataResponse getFinancialNews(HttpServletRequest request) {
        spiderService.insertFinancialNews();
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), null);
    }


    /**
     * 分页查询财经新闻
     *
     * @param request
     * @param pageIndex
     * @param pageSize
     * @param sortName
     * @param sortOrder
     * @param newsDate
     * @return
     */
    @PostMapping("/get/financialNews")
    public DataResponse getFinancialNews(HttpServletRequest request,
                                         @RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                         @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                         @RequestParam(value = "sortName", required = false) String sortName,//排序列名字
                                         @RequestParam(value = "sortOrder", required = false) String sortOrder,//排序规则(asc/desc)
                                         String newsDate
    ) {
        try {
            DhPageInfo<FinancialNewsVO> financialNewsList = spiderService.getFinancialNews(pageIndex, pageSize, sortName, sortOrder, newsDate);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), financialNewsList);
        } catch (Exception e) {
            log.error("spiderData spiderDataList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }


    /**
     * 更新财经新闻的关键字
     *
     * @return
     */
    @PostMapping("/update/keywords")
    public DataResponse updateKeyWords() {
        spiderService.updateKeyWords();
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), "更新关键字成功");
    }


}
